package services;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.LogThaoTacPOJO;

import DataAccess.LogThaoTacDA;

/**
 * Servlet implementation class LogThaoTacServlet
 */
@WebServlet("/LogThaoTacServlet")
public class LogThaoTacServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LogThaoTacServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		request.setCharacterEncoding("UTF-8");
//		response.setContentType("text/html; charset=UTF-8");
//		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		String cmd=getParameter(request, Protocol.CMD);
		System.out.println("LogThaoTacServlet cmd="+cmd);
		if(cmd.equals(Protocol.UPLOAD)){
			String nguoiDung=getParameter(request, Protocol.NGUOI_DUNG);
			String ghiChu=getParameter(request, Protocol.GHI_CHU);
			int idCay=Integer.parseInt(getParameter(request, Protocol.ID_CAY));
			int loai=Integer.parseInt(getParameter(request, Protocol.MSG_TYPE));
			new LogThaoTacDA().insert(nguoiDung, ghiChu, idCay, loai);
		}else if(cmd.equals(Protocol.DOWNLOAD)){
			int idCay=Integer.parseInt(getParameter(request, Protocol.ID_CAY));
			ArrayList<LogThaoTacPOJO> lstLog=new LogThaoTacDA().getByIdCay(idCay);
			String rs=LogThaoTacPOJO.createXML(lstLog);
			out.print(rs);
			System.out.println(rs);
		}
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
